//
//  ChangePassViewController.m
//  TerritoryShopkeeper
//
//  Created by 李小龙 on 2018/3/30.
//  Copyright © 2018年 ZDQK. All rights reserved.
//

#import "ChangePassViewController.h"

@interface ChangePassViewController ()
@property (weak, nonatomic) IBOutlet UITextField *passwordOldTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordNewTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordNewAgainTF;
@end

@implementation ChangePassViewController
#pragma mark - IBAction
- (IBAction)oldPassButtonAction:(id)sender {
    UIButton *button = (UIButton *)sender;
    [button setSelected:!button.selected];
    [self.passwordOldTF setSecureTextEntry:!button.selected];
}
- (IBAction)newPassButtonAction:(id)sender {
    UIButton *button = (UIButton *)sender;
    [button setSelected:!button.selected];
    [self.passwordNewTF setSecureTextEntry:!button.selected];
}
- (IBAction)newPassAgainButtonAction:(id)sender {
    UIButton *button = (UIButton *)sender;
    [button setSelected:!button.selected];
    [self.passwordNewAgainTF setSecureTextEntry:!button.selected];
}

#pragma mark - UIViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"修改密码";
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction:)];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIColor ThemeColor] forKey:NSForegroundColorAttributeName];
    [right setTitleTextAttributes:dic forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = right;
    
    [self hiddenShadowImage:NO];
}
#pragma mark - Action
- (void)rightBarButtonItemAction:(id)sender {
    if (!TextIsNotNil(self.passwordOldTF.text)) {
        [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"请输入旧密码" animated:YES];
        return;
    }
    if (!TextIsNotNil(self.passwordNewTF.text)) {
        [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"请输入新密码" animated:YES];
        return;
    }
    if (!TextIsNotNil(self.passwordNewAgainTF.text)) {
        [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"请再次输入新密码" animated:YES];
        return;
    }
    if (![self.passwordNewTF.text isEqualToString:self.passwordNewAgainTF.text]) {
        [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"两次输入的新密码不一致" animated:YES];
        return;
    }
    
    [self.view endEditing:YES];
    __weak typeof(self) weakSelf = self;
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    [MYAFNetworking POST:URL_Save_Savempass parameters:@{@"mpass":self.passwordOldTF.text,@"nmpass":self.passwordNewTF.text,@"qrpass":self.passwordNewAgainTF.text} success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            [MBProgressHUD myShowHUDAddedTo:weakSelf.view labelText:@"修改成功" animated:YES];
            [weakSelf disMissSelf];
        } else {
            [MBProgressHUD showError:weakSelf.view title:responseObject[@"message"] code:responseObject[@"code"] animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:weakSelf.view animated:YES];
        [MBProgressHUD showError:weakSelf.view title:@"" code:@"" animated:YES];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
